﻿using ConfExpert.Forms.Base;
using ConfExpert.Utility;
using DevTeam;
using System;
using System.Data;

namespace ConfExpert.Forms.SatinAlma.YeniYapi
{
    public partial class YeniKodUret : XForm
    {
        public YeniKodUret()
        {
            InitializeComponent();
        }

        private void KumasTuret_Load(object sender, EventArgs e)
        {
            gleProduct.Edit.Assign(RI(Rp.V_Kumas));
            gleKomp.Properties.Assign(RI(Rp.Kompozisyon));
            gleKalite.Properties.Assign(RI(Rp.Kalite));
            gleFinish.Properties.Assign(RI(Rp.Finish));

            gleProduct.EditValueChanged += gleProduct_EditValueChanged;
            btnKaydet.ItemClick += btnKaydet_ItemClick;
        }

        void btnKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            string res = ExecuteScalar("InsKumasTuret", gleProduct.EditValue.ToInt(), 
                txtStokKodu.Text, 
                gleKomp.EditValue.ToStr(), 
                txtKonst.Text, 
                gleKalite.EditValue.ToInt(), 
                txtKaliteAciklama.Text, 
                gleFinish.EditValue.ToInt()).ToStr();

            if (res != string.Empty)
                ShowErrorMessage(res);
            else
                AppSession.MainForm.RefreshData();
        }

        void gleProduct_EditValueChanged(object sender, EventArgs e)
        {
            txtStokKodu.EditValue =
            txtKonst.EditValue =
            txtKaliteAciklama.EditValue =
            gleKomp.EditValue =
            gleKalite.EditValue =
            gleFinish.EditValue = null;

            DataRow r = AppSession.Lookups.Tables["V_Kumas"].Select("ProductId="+ gleProduct.EditValue.ToInt())[0];

            if (r["Name"].ToStr() != string.Empty)
                txtStokKodu.EditValue = r["Name"].ToStr();
            if (r["KompozisyonId"].ToInt() > 0)
                gleKomp.EditValue = r["KompozisyonId"].ToInt();
            if (r["Konstruksiyon"].ToStr() != string.Empty)
                txtKonst.EditValue = r["Konstruksiyon"].ToStr();
            if (r["KaliteId"].ToInt() > 0)
                gleKalite.EditValue = r["KaliteId"].ToInt();
            if (r["KaliteAciklama"].ToStr() != string.Empty)
                txtKaliteAciklama.EditValue = r["KaliteAciklama"].ToStr();
            if (r["FinishId"].ToInt() > 0)
                gleFinish.EditValue = r["FinishId"].ToInt();
        }
    }
}
